"""
文本统计程序 - v0.2版本
实现基础功能：统计字符数、单词数、句子数
"""

import sys
import re

def count_characters(text):
    """统计字符数（包括空格和标点）"""
    return len(text)

def count_words(text):
    """统计单词数"""
    # 使用正则表达式匹配单词（字母数字组合）
    words = re.findall(r'\b[a-zA-Z]+\b', text)
    return len(words)

def count_sentences(text):
    """统计句子数"""
    # 以 . ! ? 作为句子结束标志
    sentences = re.split(r'[.!?]+', text)
    # 过滤空字符串
    sentences = [s.strip() for s in sentences if s.strip()]
    return len(sentences)

def read_file(filename):
    """读取文件内容"""
    try:
        with open(filename, 'r', encoding='utf-8') as file:
            return file.read()
    except FileNotFoundError:
        print(f"错误：文件 {filename} 未找到")
        return None
    except Exception as e:
        print(f"读取文件时出错：{e}")
        return None

def main():
    if len(sys.argv) != 3:
        print("用法: python wc_basic.py [参数] [文件名]")
        print("参数:")
        print("  -c  统计字符数")
        print("  -w  统计单词数")
        print("  -s  统计句子数")
        print("  -a  统计所有基本信息")
        return
    
    param = sys.argv[1]
    filename = sys.argv[2]
    
    text = read_file(filename)
    if text is None:
        return
    
    if param == '-c':
        char_count = count_characters(text)
        print(f"字符数: {char_count}")
    
    elif param == '-w':
        word_count = count_words(text)
        print(f"单词数: {word_count}")
    
    elif param == '-s':
        sentence_count = count_sentences(text)
        print(f"句子数: {sentence_count}")
    
    elif param == '-a':
        char_count = count_characters(text)
        word_count = count_words(text)
        sentence_count = count_sentences(text)
        
        print(f"字符数: {char_count}")
        print(f"单词数: {word_count}")
        print(f"句子数: {sentence_count}")
    
    else:
        print("错误：无效参数")
        print("可用参数: -c, -w, -s, -a")

if __name__ == "__main__":
    main()